Session 1 Homework

Try these homework questions to revise the content of session 1.

  1. What would be the result of evaluating the following in R?
    1. 2 + 3 * 4
    2. (2 + 3) * 4
    3. 2 == 3
    4. x <- 32
    5. read_csv("data.csv")
    6. mean(iris$Sepal.Length)
    7. tail(iris)
    8. !FALSE
    9. sum(c(1, 2, 5, 9))
    10. class(TRUE)
    11. mean(c("cat", "dog", "fish"))
Hint You can try these in RStudio to check your answers!
  1. I’ve written the following code in R. What would be the output? What are the values of x, y, and z after running all of this code?
x <- 1:5
y <- x * 2
z <- x + y
z
  1. I’m trying to run the following code in R, but I got an error. Why? There might be more than one error.
2020_data <- read_csv("2020_data.csv")
  1. Load the mtcars data into your R environment using the data() function.
    1. What are the first 10 rows of the mtcars dataset?
    2. What is the mean of the mpg column in the mtcars dataset?
Hint For (a), head() gives the first six rows of a dataset by default. Check the documentation for the head() function to see how to get the first 10 rows. For (b), you can use the mean() function to calculate the mean of a column in a dataset.
  1. Challenge: using the iris dataset, I want to convert the Sepal.Length column to centimeters (it’s currently in inches) and add the result as a new column to iris. There are 2.54 centimeters in an inch. How would you do this in R?
Hint You can create a new column in the iris dataset by assigning a vector to a new column name. For example, iris$new_column <- ...

If you have any questions, post them on the teams group. If you’d like more practice, try out the practice questions in the course ebook.

Just for fun: customising your RStudio!

RStudio is highly customisable, and you can change the appearance of the interface to suit your preferences. Try changing the theme, font size, or layout of RStudio to make it more comfortable for you to work in. You can find these options in the RStudio settings (Tools > Global Options):

For example, you can change the theme in the ‘Appearance’ section:

Here’s the ‘dracula’ theme:

Find a theme that you like and make RStudio your own!